
package com.dsideal.cloud.cas.client.cas.entity;

import com.alibaba.fastjson.JSONObject;
import com.dsideal.cloud.auth.common.util.jwt.IJWTInfo;
import com.dsideal.cloud.auth.common.util.jwt.JWTInfo;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.HashSet;
import java.util.Set;


/**
 * @author suny
 */

public class CasUserDetails implements UserDetails {

/**
     * 用户ID
     */

    private String id;


/**
     * 用户名称
     */

    private String name;


/**
     * 登录名称
     */

    private String username;


/**
     * 登录密码
     */

    private String password;


/**
     * token
     */

    private String token;

    /**
     * 单位id
     */
    private String bureauId;

    public String getBureauId() {
        return bureauId;
    }

    public void setBureauId(String bureauId) {
        this.bureauId = bureauId;
    }

    private boolean isAccountNonExpired = true;

    private boolean isAccountNonLocked = true;

    private boolean isCredentialsNonExpired = true;

    private boolean isEnabled = true;

    private Set<CasAuthority> authorities = new HashSet<>();

    private Set<String> roles = new HashSet<>();

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return isEnabled;
    }

    @Override
    public Set<CasAuthority> getAuthorities() {
        return authorities;
    }

    public Set<String> getRoles() {
        return roles;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        isAccountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        isAccountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        isCredentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public void setAuthorities(Set<CasAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public CasUserDetails() {

    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static CasUserDetails fromJWTInfo(IJWTInfo jwtInfo) {
        CasUserDetails casUserDetails = null;

        if (jwtInfo instanceof JWTInfo) {
            JWTInfo userInfo = (JWTInfo) jwtInfo;
            casUserDetails = new CasUserDetails();
//            casUserDetails.setId(Long.parseLong(userInfo.getId()));
            casUserDetails.setName(userInfo.getName());
            casUserDetails.setUsername(userInfo.getUniqueName());
          /*  casUserDetails.setAuthorities(new HashSet<CasAuthority>() {
                {
                    for (String authority : userInfo.getAuthorities()) {
                        add(new CasAuthority(authority));
                    }
                }
            });
            casUserDetails.setRoles(new HashSet<String>() {
                {
                    for (String role : userInfo.getRoles()) {
                        add(role);
                    }
                }
            });*/
        }

        return casUserDetails;
    }

}

